<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/4/18
 * Time: 18:40
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */

namespace app\admin\controller;


use app\common\model\MessageTemplate;
use app\common\model\NoticeMessage;
use app\common\model\RollMessage;
use app\Request;
use app\services\wechat\Miniprogram;
use app\services\wechat\Offiaccount;

class  Message
{
    public function index(Request $request)
    {
        $messageTemplate = new  MessageTemplate();

        $param = $request->param();
        $where['type'] = 1;
        if (isset($param['type']) && !empty($param['type'])) {
            $where['type'] = $param['type'];
        }
        $list = $messageTemplate->where($where)->select()->toArray();
        return success('消息模板', $list);
    }

    public function editMessage(Request $request)
    {
        $param = $request->param();
        $messageTemplate = new  MessageTemplate();
        if (isset($param['tid']) && !empty($param['tid'])) {
            $data['tid'] = $param['tid'];
        }
        if (isset($param['template_name']) && !empty($param['template_name'])) {
            $data['template_name'] = $param['template_name'];
        }

        $messageTemplate->where(['id' => $param['id']])->update($data);

        return success('修改成功');
    }

    public function status(Request $request)
    {
        $param = $request->param();
        $messageTemplate = new  MessageTemplate();
        $messageTemplate->where(['id' => $param['id']])->update(['status' => $param['status']]);

        return success('修改成功');
    }

    public function templateContent(Request $request)
    {
        $param = $request->param();
        if ($param['type'] == 1){
            $offiaccount = new Offiaccount();
            $result = $offiaccount->templateId();
            return success('模板',$result);
        }else{
            $miniprogram = new Miniprogram();
            $result = $miniprogram->templateId();
            return success('模板',$result);
        }

    }

    public function rollMessageList(Request $request)
    {

        $rollMessageModel = new RollMessage();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit + $start;
        }
        $list = $rollMessageModel->limit($start,$limit)->select()->toArray();
        $count  = $rollMessageModel->count();
        return success('消息模板', ['list' => $list,'count' => $count]);

    }
    public function addRollMessage(Request $request)
    {
        $param = $request->param();
        $data = [
            'title' => $param['title'],
            'create_time' => time(),
        ];
        if (isset( $param['desc'])){
            $data['desc'] = $param['desc'];
        }
        $rollMessageModel = new RollMessage();
        $rollMessageModel->insert($data);
        return success('添加成功');
    }
    public function delRollMessage(Request $request)
    {
        $param = $request->param();
        $rollMessageModel = new RollMessage();
        $rollMessageModel->where(['id' => $param['id']])->delete();
        return success('添加成功');
    }

    public function chatMessageLog(Request $request){
        $param = $request->param();
        $userMessageModel = new UserMessage();
        $limit = 10;
        $start = 0;
        $where = [];
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        if (isset($param['uid']) && !empty($param['uid'])){
            $where[] = ['send_uid|receive_uid','=',$param['uid']];
        }
        if (isset($param['nickname']) && !empty($param['nickname'])){
            $where[] = ['u.nickname|ru.nickname','like','%'.$param['nickname'].'%'];
        }
        $list = $userMessageModel->alias('um')
            ->leftJoin('user u','um.send_uid  = u.id')
            ->leftJoin('user ru','um.receive_uid  = ru.id')
            ->field('um.*,u.nickname as send_nickname,u.avatar as send_avatar,ru.nickname as receive_nickname,ru.avatar as receive_avatar')
            ->limit($start,$limit)->order('create_time','desc')->select()->toArray();
        $count = $userMessageModel->limit($start,$limit)->count();
        return success('成功',['list'=>$list,'count' => $count]);
    }

    public function noticeMessage(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }

        if (isset($param['type']) && $param['type'] == 2){
            $where[] = ['type' ,'in',[1,2,3,4,5,6,8,9,10,11,12,13,14,15]];
        }else{
            $where[] = ['type' ,'=',7];
        }
        $noticeMessageModel = new  NoticeMessage();
        $list = $noticeMessageModel-> alias('n')
            ->leftJoin('user u','n.uid  = u.id')
            ->leftJoin('user ru','n.receive_uid  = ru.id')
            ->leftJoin('service s','n.service_id  = s.id')
            ->leftJoin('dazi d','n.dazi_id  = d.id')
            ->leftJoin('circle c','n.circle_id  = c.id')
            ->leftJoin('circle_comment cc','n.comment_id  = cc.id')
            ->field('n.*,u.nickname ,u.avatar, 
            ru.nickname as receive_nickname,
            ru.avatar as receive_avatar,
            s.title as service_title,
            d.title as dz_title,
            c.title as circle_title')
            ->where($where)
            ->limit($start,$limit)
            ->select()->toArray();

        $count = $noticeMessageModel-> alias('n')
            ->leftJoin('user u','n.uid  = u.id')
            ->leftJoin('user ru','n.receive_uid  = ru.id')
            ->leftJoin('service s','n.service_id  = s.id')
            ->leftJoin('dazi d','n.dazi_id  = d.id')
            ->leftJoin('circle c','n.circle_id  = c.id')
            ->leftJoin('circle_comment co','n.comment_id  = co.id')
            ->where($where)
            ->count();
        return success('消息通知',['list' => $list,'count' => $count]);
    }

    public function sendNoticeMessage(Request $request){
        $param = $request->param();
        if ($param['type'] == 1){
            $userModel = new \app\common\model\User();
            $userArray = $userModel->where(['status' => 1])->field('id')->select()->toArray();
            foreach ($userArray as $key => $value){
                $data[$key]['content'] = $param['content'];
                $data[$key]['receive_uid'] = $value['id'];
                $data[$key]['type'] =7;
                $data[$key]['create_time'] = time();
            }
        }else{
            $uidArray =  explode(',',$param['uid']);
            foreach ($uidArray as $key => $value){
                $data[$key]['content'] = $param['content'];
                $data[$key]['receive_uid'] = $value;
                $data[$key]['type'] = 7;
                $data[$key]['create_time'] = time();
            }
        }
        $noticeMessageModel = new  NoticeMessage();
        $noticeMessageModel->insertAll($data);
        return success('成功');
    }

}